import { loginAsync } from '@/api/login'

const userModule = {
  state: {
    username: null,
    password: null,
  },
  mutations: {
    SET_USERINFO(state, payload) {
      state.userinfo = payload
    },
    SET_TOKEN(state, payload) {
      state.token = payload
    },
    RESET(state) {
      state.userinfo = null
      state.token = null
    },
  },
  actions: {
    login(store, payload) {
      return loginAsync(payload)
        .then(data => {
          const { username, password, avatarUrl, token } = data
          store.commit('SET_USERINFO', { username, avatarUrl, password })
          store.commit('SET_TOKEN', { token })
        })
        .catch(err => {
          console.log('异常：', err)
          store.commit('RESET')
        })
    },

  },
}

export default userModule
